﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DoNetDrive.Protocol.Door.Test
{
    public class SettingTool
    {
        public static SettingInfo GetSetting()
        {
            var info = new SettingInfo();

            Type exampleType = typeof(SettingInfo);
            PropertyInfo[] properties = exampleType.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                string sValue = ConfigurationManager.AppSettings[property.Name];
                if (!string.IsNullOrWhiteSpace(sValue))
                {
                    property.SetValue(info, sValue);
                }
            }
            return info;
        }

        public static void SetSetting(SettingInfo info)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            Type exampleType = typeof(SettingInfo);
            PropertyInfo[] properties = exampleType.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                object value = property.GetValue(info);
                SetConfigKey(config, property.Name, value.ToString());
            }
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
        private static void SetConfigKey(Configuration config, string key, string sValue)
        {
            var kv = config.AppSettings.Settings;
            if (kv[key] == null)
                kv.Add(key, sValue);
            else
                kv[key].Value = sValue;
        }
    }

    public class SettingInfo
    {
        public SettingInfo()
        {
            TCPClientAddr = "192.168.1.150";
            TCPClientPort="8000";
            UDPLocalPort = "9000";
            UDPPort = "8101";
            DriveSN = "0000000000000000";
            DrivePassword = "FFFFFFFF";
            ConnType = "2";
            ProtocolType = "0";
            LocalIP = "";
            TCPServerPort = "9000";
            ToolLanguage = "0";
        }
        public string TCPClientAddr { get; set; }
        public string TCPClientPort { get; set; }
        public string UDPLocalPort { get; set; }

        public string UDPPort { get; set; }

        public string DriveSN { get; set; }

        public string DrivePassword { get; set; }
        public string ConnType { get; set; }

        public string ProtocolType { get; set; }

        public string LocalIP { get; set; }

        public string TCPServerPort { get; set; }

        public string ToolLanguage { get; set; }
    }
}
